<?php
/**
 * Created by PhpStorm.
 * User: dayu
 * Date: 2019/12/24
 * Time: 10:15
 */

namespace app\api;

use data\service\SecretToken;
use think\cache\driver\Redis;
use think\Request;

class Before
{
    public function run()
    {
        if (config('token')['type'] && strstr(request()->url(), "/api/")) {
//            if (strstr(request()->url(), "/api/disposeToken")) {
//                if (strtolower($_SERVER['REQUEST_METHOD']) != 'post') {
//                    return false;
//                }
//                $secret = request()->post('secret', '');
//                if (empty($secret)) {
//                    exit(json_encode(return_json_data(AjaxReturn(PARAMS_DEFICIENCY, '', true), 604)));
//                }
//                $db = new SecretToken();
//                $data = $db->disposeToken($secret, true);
//                if ($data === false) {
//                    $sre = return_json_data(AjaxReturn(TOKEN_CREATE_ERROR, '', true), 602);
//                } else {
//                    $sre = return_json_data(getNonceStr(10) . base64_encode(json_encode($data)), 200);
//                }
//                exit($sre);
//            }
            $boole = (new Redis())->get(request()->header("access-token"));
            if (!$boole) {
                exit(json_encode(return_json_data(AjaxReturn(TOKEN_VALIDATION_ERROR, '', true), 603)));
            }
            return true;
        }
    }
}